﻿using OperationPlugins.Mvc.Filters;
using System.Web.Mvc;
using TimeProject.Engine;
using TimeProject.Engine.DbContent;
using TimeProject.Engine.DbModels;
using TimeProject.WebSite.ViewModels;
using WebMatrix.WebData;

namespace TimeProject.WebSite.Controllers
{
    public partial class TimeProjectController
    {
        [HttpGet]
        [CurrentOperation(TimeProjectOperations.DeleteAccount)]
        public ActionResult DeleteAccount()
        {
            DeleteAccountViewModel vm = new DeleteAccountViewModel();
            return View(vm);
        }

        [HttpPost]
        [CurrentOperation(TimeProjectOperations.DeleteAccount)]
        public ActionResult DeleteAccount(DeleteAccountViewModel vm)
        {
            return Json(vm, () =>
            {
                User user = Db.GetUser(UserId);

                user.SoftDelete();

                Db.UserActivities.Add(new UserActivity
                {
                    UserId = user.Id,
                    Type = UserActivityType.DeleteAccount,
                    Created = RequestContext.Current.RequestTimestamp,
                    Creator = user.Username
                });

                if (SaveDbContext(vm))
                {
                    WebSecurity.Logout();
                    vm.RedirectTo("Login");
                }
            });
        }

    }
}